//
//  OrderLogisticsViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/5/30.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "OrderLogisticsViewController.h"
#import "OrderLogisticsCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "UserInfoModel.h"
#import "ExpressInfoListModel.h"

NSString * const  OrderLogisticsCellIdentifier = @"OrderLogisticsCellIdentifier";
@interface OrderLogisticsViewController ()
@property(nonatomic, copy) NSString * orderId;
@property(nonatomic, strong) ExpressInfoListModel * listModel;
@property(nonatomic, assign) LogisticsVCType  type;
@property(nonatomic, copy)NSString * order_goods_id;
@end

@implementation OrderLogisticsViewController
- (instancetype)initOrderId:(NSString *)orderId withVCType:(LogisticsVCType )type{
    self = [super init];
    if (self) {
        self.orderId = orderId;
        self.type = type;
    }
    return self;
}
- (instancetype)initOrderId:(NSString *)orderId withVCType:(LogisticsVCType )type withGoodsId:(NSString *) goodsId{
    self = [super init];
    if (self) {
        self.orderId = orderId;
        self.type = type;
        self.order_goods_id = goodsId;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationItemTitle:@"物流信息"];
    [self requestOrderDetail];
 
}
- (void)requestOrderDetail{
    NSDictionary * params = @{@"order_id":self.orderId,@"token":UserInfo.userToken};
    
    NSString* pathStr = @"";
    if (self.type ==LogisticsVCType_order ) {
        pathStr = @"/Order/getExpressInfo";
    }else if(self.type == LogisticsVCType_idOrMatn){
        pathStr= @"Otherorder/getExpressInfo";
    }else if(self.type == LogisticsVCType_conOrRe){
        pathStr= @"Consignorder/getExpressInfo";
        
    }else if (self.type == LogisticsVCType_conOrReSingle){
         pathStr= @"Consignorder/getExpressInfo";
        //物流类型 0-寄出物流 1-回寄物流 （默认不传为0）
        //订单商品ID（type=1时必填） order_goods_id
        params = @{@"order_id":self.orderId,@"token":UserInfo.userToken,@"type":@"1",@"order_goods_id":self.order_goods_id};
    }
    WEAKSELF
    [[MainApi sharedInstance] postPath:pathStr params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        NSLog(@"%@===",responseObject);
        if (responseObject && !error) {
            strongSelf.listModel = [[ExpressInfoListModel alloc]initWithDictionary:responseObject error:nil];
        }
        [strongSelf.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([OrderLogisticsCell class]) bundle:nil] forCellReuseIdentifier:OrderLogisticsCellIdentifier];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.listModel.data count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat   height = 0;
    
   height = [tableView fd_heightForCellWithIdentifier:OrderLogisticsCellIdentifier cacheByIndexPath:indexPath configuration:^(OrderLogisticsCell *cell) {
        [self configureCell:cell atIndexPath:indexPath];
    }];
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    OrderLogisticsCell * tempCell = [tableView dequeueReusableCellWithIdentifier:OrderLogisticsCellIdentifier];
    [self configureCell:tempCell atIndexPath:indexPath];
    if (indexPath.row == 0) {
        [tempCell confightFistCell];
    }else if (indexPath.row == [self.listModel.data count] -1){
        [tempCell confightEndCell];
    }
    cell = tempCell;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)configureCell:(OrderLogisticsCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.fd_enforceFrameLayout = NO; // Enable to use "-sizeThatFits:"
    
    ExpressInfoModel * infoModel  = self.listModel.data[indexPath.row];
    cell.entity =  infoModel;
}

@end
